import java.util.ArrayList;
import java.util.List;

public class BinaryTree {
    static class TreeNode{
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }
    public TreeNode root;//指向一个根节点
    public TreeNode createTree(){
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
    }

    public void preOrder(TreeNode root){
        if (root == null) {
            return;
        }
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }

    public void inOrder(TreeNode root){
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }
    public void lastOrder(TreeNode root){
        if (root == null) {
            return;
        }
        lastOrder(root.left);
        lastOrder(root.right);
        System.out.print(root.val+" ");
    }
    List<TreeNode> ret = new ArrayList<>();
    public List<TreeNode> preOrder2(TreeNode root){ //将前序遍历的结果存储到List中
        if (root == null) {
            return ret;
        }
        ret.add(root);
        List<TreeNode> leftTree = preOrder2(root.left);
        ret.addAll(leftTree);
        List<TreeNode> rightTree = preOrder2(root.right);
        ret.addAll(rightTree);
        return ret;
    }
    public int size = 0;
    public void nodeSize(TreeNode root){ //计算该二叉树中的所有节点数
        if (root == null) {
            return ;
        }
        size++;
        nodeSize(root.left);
        nodeSize(root.right);
    }

    public int nodeSize2(TreeNode root){
        if (root == null) {
            return 0;
        }
        return nodeSize2(root.left) + nodeSize2(root.right) + 1;
    }
    public int leafSize = 0;
    public void getLeafSize(TreeNode root){ //叶子节点的个数
        if (root == null) {
            return;
        }
        if (root.left ==null && root.right == null){
            leafSize++;
        }
        getLeafSize(root.left);
        getLeafSize(root.right);
    }
    public int getLeafSize2(TreeNode root){
        if (root == null) {
            return 0;
        }
        if (root.left ==null && root.right == null){
            return 1;
        }
        return getLeafSize2(root.left) + getLeafSize2(root.right);
    }

    public int getKLevelNodeCount(TreeNode root,int k){ //第k层叶子的节点个数
        if (root == null) {
            return 0;
        }
        if (k == 1){ //第k层的节点相对与自己来说是第一层
            return 1;
        }
        //如果不是1，则继续向下层遍历，并且返回左树和右树的第k层叶子节点和
        return getKLevelNodeCount(root.left,k-1) + getKLevelNodeCount(root.right,k-1);
    }
    public int height = 0;
    public int lower = 0;
    public int getHeight(TreeNode root){ //计算树的层数
        if (root == null) {
            return 0;
        }
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);
        return (leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1);
        //代码走到树的最底层开始加1，一直向上返回即可得到累计层数加1的值
    }

    public boolean find(TreeNode root,char key){ //查找树中有没有需要找的值
        if (root == null){
            return false;
        }
        if (root.val == key){
            return true;
        }
        boolean leftVal = find(root.left,key);
        if (leftVal == true){
            return true;
        }
        boolean rightVal = find(root.right,key);
        if (rightVal == true){
            return true;
        }
        return false;
    }
    public boolean isSame(TreeNode root,TreeNode root1){ //判断两棵树是否相同
        if (root != null && root1 == null || root == null && root1 != null){
            return false;
        }
        if (root == null && root1 == null){
            return true;
        }
        if (root.val != root1.val) {
            return false;
        }
        return isSame(root.left,root1.left) && isSame(root.right,root1.right);
    }
    public boolean isSonTree(TreeNode root,TreeNode subRoot){ //判断两个树是不是其中一个的子树
        if (root == null){
            return false;
        }
        if (isSame(root,subRoot)){
            return true;
        }
        if (isSonTree(root.left,subRoot)){
            return true;
        }
        if (isSonTree(root.right,subRoot)){
            return true;
        }
        return false;
    }
    public TreeNode invertTree(TreeNode root){  //翻转二叉树
        if (root == null){
            return  null;
        }
        TreeNode ret = root.left;
        root.left = root.right;
        root.right = ret;
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }
    public boolean isBalanced(TreeNode root) { //求该数是否为平衡树
        if(root == null){
            return true;
        }
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);
        if(Math.abs(leftHeight - rightHeight )<= 1){// 求绝对值
            if(isBalanced(root.left)&& isBalanced(root.right)){
                return true;
            }
        }
        return false;
    }
    public boolean isSymmetric(TreeNode root){
        if (root == null){
            return true;
        }
        return isSymmetricChild(root.left,root.right);
    }
    public boolean isSymmetricChild(TreeNode p,TreeNode q){
        if (p == null && q != null || p != null && q == null){
            return false;
        }
        if (p == null && q == null){
            return true;
        }
        if (p.val != q.val){
            return false;
        }
        if (p.right.val == q.left.val && p.left.val == q.right.val){
            return true;
        }
        return isSymmetricChild(p.left,q.right) && isSymmetricChild(p.right,q.left);
    }
    public int i = 0;
    public TreeNode createTree1(String str){ //以前序遍历创建二叉树
        TreeNode root = null;
        if (str.charAt(i) != '#'){
            root = new TreeNode(str.charAt(i));
            i++;
            //当i不为#时，i继续向后遍历，并一直赋值给左树，当遇到#时，向上返回空值，此时返回
            //给上一层，由于左树已经遍历完成，故继续向后，即给上一层的右树赋值，i++。
            root.left = createTree1(str);
            root.right = createTree1(str);
        }else {
            i++;
        }
        return root;
    }
}
